<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <!-- 准备好一个容器，承载整个react -->
  <div id="root"></div>

  <!-- react核心库 -->
  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <!-- 支持react操作dom -->
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <!-- 将jsx转为js -->
  <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
  <!-- 对组件标签属性进行限制 -->
  <script src="https://cdn.bootcss.com/prop-types/15.6.1/prop-types.js"></script>

  <script type="text/babel">
    class Life extends React.Component {
      state = {
        opacity: 1
      }
      timer = null //定时器
      render() {
        console.log('Life-render')
        return (
          <div>
            <h1 style={{opacity: this.state.opacity}}>react学会不怎么办</h1>
            <input type="button" value="卸载组件" onClick={this.unloadComponent}/>
          </div>
        )
      }

      // 组件挂载完毕
      componentDidMount(){
        console.log('Life-componentDidMonut')
        this.timer = setInterval(() => {
          this.setState({
            opacity: this.state.opacity - 0.1
          })
          if(this.state.opacity <= 0)  this.setState({opacity: 1})
        }, 100)
      }

      // 组件将要被卸载
      componentWillUnmount(){
        console.log('Life-componentWillUnmount')
        clearInterval(this.timer)
      }

      // 卸载组件
      unloadComponent = () => {
        ReactDOM.unmountComponentAtNode(document.getElementById('root'))
      }
    }
    ReactDOM.render(<Life />, document.getElementById('root'))
  </script>
</body>

</html>